Ana içeriğe geç

Creating the Attester Account 🏢

Let's get started! In the previous step, we set up the necessary file locations. Now, it's time to create the account of our Attester character on the blockchain. 🎉

Do you have an Attester address?

There are two main components here: the public address and, of course, the mnemonic passphrase. If you've already created these details using a virtual wallet on testnet, there's no need to create a new account. 🎯

An account on the KILT blockchain is one of the main components that interact with the blockchain. 🌐 This account has various features. One of them is the address, the account address. 💳 This address is used to pay transaction fees and deposits.

📒 KILT Account

A KILT account is essentially a combination of several cryptographic elements:

  • A public address 🌐
  • A key pair for signing 🔑
🎓 MNEMONIC

The mnemonic passphrase usually consists of 12, 18, or 24 words. 📝 It provides access to your wallet and assets. For instance, it could be something like "apple candle mountain bicycle flower". It's critical for security, so it's crucial to keep this passphrase safe! 🔒

alternative text

Creating the Account 🛠️

We use the addFromMnemonic() function on the KiltKeyringPair class to create an account. 🌟 This generates a 12-word mnemonic passphrase for us.

📘 Polkadot.js

The KILT SDK is built on the polkadot.js library. 🛠️ You'll encounter it frequently in your work. This library offers many useful features for KILT and other substrate-based blockchains. 🌐

Let's Start Coding

Now that we've learned the basics and understand why we're doing what we're doing, we can start coding:

Library Integration

attester/genarateAccount.ts
import { config as envConfig } from "dotenv"

import * as Kilt from "@kiltprotocol/sdk-js"

First, as is necessary in other software projects, we integrate our libraries into our code. If we examine these lines of code one by one:

  • First, we start by calling the envConfig method from the dotenv library as config. Thanks to this library, we can access data saved in an external .env file within our code using the config keyword.
  • On the next line, we see that all features of the KILT SDK are called from the @kiltprotcol/sdk-js library and named as Kilt. The * structure in this line indicates that we're integrating all the code from the library, while the as Kilt structure allows us to access it using the name Kilt without using the long library name @kiltprotocol/sdk-js every time.

The generateAccount Function

attester/genarateAccount.ts
export function generateAccount(
mnemonic = Kilt.Utils.Crypto.mnemonicGenerate()
): {
account: Kilt.KiltKeyringPair
mnemonic: string
} {

After adding our libraries, we can proceed. The first operation in this function is to create the mnemonic key. The generateAccount() function shown above does precisely this. If we examine it step by step:

  • First, we create a function that can be accessed from outside the code using the export method. We can give this function any name we want, but for now, we've named it generateAccount().
  • On the next line, for the first time, we access the KILT SDK in our code. Here, we define a variable named mnemonic using the mnemonicGenerate() method from the Utils.Crypto package of the KILT SDK. This way, we generate the mnemonic key.
  • In the next line, we encounter a structure :{ ... } that might be unfamiliar. This structure is used to define the type of value the function will return. Inside this structure:
    • The line account: Kilt.KiltKeyringPair indicates that the returned object will have a property named account, and its type will be Kilt.KiltKeyringPair. This value represents the key pair used for signing operations, which we briefly mentioned earlier.
    • The line mnemonic: string indicates that the returned object will have another property named mnemonic, and its type will be a string. This is the mnemonic passphrase we're trying to generate.
attester/genarateAccount.ts
  const keyring = new Kilt.Utils.Keyring({
ss58Format: 38,
type: 'sr25519'
})

The next code structure within our function is the creation of the keyring variable. This structure is written right after the code discussed above and creates the keyring value.

What is a keyring?

The term keyring in the code is used to create a keyring. A keyring is a data structure that facilitates the management of various key pairs (public and private keys). This structure is typically used to securely and efficiently store and manage keys for cryptographic operations.

If we examine it line by line:

  • First, we see that a new object is created from the Kilt.Utils.Keyring method and assigned to the keyring variable. This object has two parameters:
    • ss58Format: Specifies the format in which the key is encoded. SS58 is commonly used in Substrate-based blockchains. We've set its value to 38 for now.
    • type: Specifies the user's cryptographic algorithm. We've chosen the value sr25519 here, which is commonly used in Substrate-based blockchains.
attester/genarateAccount.ts
  return {
account: keyring.addFromMnemonic(mnemonic) as Kilt.KiltKeyringPair, mnemonic
}
}

The final stage of our function is the return structure, which provides an output. This output is the account of the Attester. It contains two values:

  • The addFromMnemonic(mnemonic) method creates a key pair using the given mnemonic passphrase and adds it to the keyring.
  • The mnemonic value is used to save the mnemonic passphrase of the address.

With this, we've created our account. All we need to do now is call the function and ensure it works correctly.

Main Program Section

We need a structure to call the function we created in the upper section. This structure indicates what will happen when the code runs standalone without other modules.

attester/genarateAccount.ts
if (require.main === module) {
;(async () => {
envConfig()

As indicated above, we first check if the running file is the main program within an if loop. If it's the main program, we call the envConfig value to integrate our data from the .env file into our project.

attester/genarateAccount.ts
    try {
await Kilt.init()
.
.
.
}catch (e){
console.log('Error while setting up attester account')
throw e
}
})()
}

Then, within the if function, we try to start the KILT-SDK with a try structure. All the code we continue to write will be located within this try structure.

When we go further down, we see a catch structure to detect and print errors to the console.

attester/genarateAccount.ts
      const { mnemonic, account } = generateAccount()

Inside the try structure, we see that we call our generateAccount function and save the mnemonic and account values from it into a variable.

attester/genarateAccount.ts
       console.log('Save to .env file before proceeding!\n\n')
console.log(`ATTESTER_ACCOUNT_MNEMONIC="${mnemonic}"`)
console.log(`ATTESTER_ACCOUNT_ADDRESS="${account.address}"\n\n`)

The above-mentioned code writes these values one by one to the terminal.

bilgi

Let's look at how the generateAccount.ts file we wrote operates with all the code together.

attester/generateAccount.ts
import { config as envConfig } from 'dotenv'

import * as Kilt from '@kiltprotocol/sdk-js'

export function generateAccount(
mnemonic = Kilt.Utils.Crypto.mnemonicGenerate()
): {
account: Kilt.KiltKeyringPair
mnemonic: string
} {
const keyring = new Kilt.Utils.Keyring({
ss58Format: 38,
type: 'sr25519'
})
return {
account: keyring.addFromMnemonic(mnemonic) as Kilt.KiltKeyringPair,
mnemonic
}
}

// Don't execute if this is imported by another file.
if (require.main === module) {
;(async () => {
envConfig()

try {
await Kilt.init()

const { mnemonic, account } = generateAccount()
console.log('save to mnemonic and address to .env to continue!\n\n')
console.log(`ATTESTER_ACCOUNT_MNEMONIC="${mnemonic}"`)
console.log(`ATTESTER_ACCOUNT_ADDRESS="${account.address}"\n\n`)
} catch (e) {
console.log('Error while setting up attester account')
throw e
}
})()
}
  • Environment Configuration: The code imports the dotenv package to manage environment variables, allowing for the configuration of settings outside the codebase.
  • KILT SDK Integration: The code imports the KILT SDK, which is a toolkit for building decentralized identity and credential systems.
  • Account Generation Function: The generateAccount function is defined to:
    • Generate a mnemonic if none is provided. A mnemonic is a series of words that represent a private key.
    • Use the generated or provided mnemonic to create a new account using KILT's keyring utilities.
    • Return both the account and the mnemonic.
  • Execution Block: If this script is run directly (not imported elsewhere):
    • It initializes the KILT SDK.
    • It generates a new account using the generateAccount function.
    • It prints out the mnemonic and account address, suggesting the user save them to the .env file for future use.
  • Error Handling: If any errors occur during the execution, they are caught, logged, and then re-thrown.

In essence, this script is a utility for setting up a new account on the KILT network, providing the user with the necessary mnemonic and account address to continue their operations.

Let's Run Our Code!

Now we can run our code to obtain the address and mnemonic passphrase of our Attester character.

To run the code, make sure you're in the kilt-rocks folder in the terminal, then run the following code:

yarn ts-node ./attester/generateAccount.ts
Don't Forget to Save!

The values printed to the console need to be copied and saved to the .env file in the same format.

dotenv
WSS_ADDRESS=wss://peregrine.kilt.io
ATTESTER_ACCOUNT_MNEMONIC="MNEMONIC PASSPHRASE"
ATTESTER_ACCOUNT_ADDRESS="ADDRESS"
We're now ready to get PILT Coins!

Now that we've created our accounts, we have a structure to pay transaction fees and deposits. But there's one thing missing: what we'll pay with! To do this, you can enter the account_address value generated when the code runs into this link and transfer from the faucet.

alternative text